package com.mmall.controller.backend;

import com.mmall.common.Const;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/manage/user/")
public class UserManageController {
    @Autowired
    private IUserService iUserService;

    public ServerResponse<User> login(HttpSession session, String username, String password){
        ServerResponse<User> serverResponse = iUserService.login(username, password);
        if (serverResponse.isSuccess()){
            User user = serverResponse.getData();
            if (user.getRole() == Const.Role.ROLE_ADMIN){
                session.setAttribute(Const.CURRENT_USER, user);
                return serverResponse;
            }else {
                return ServerResponse.createByErrorMessage("你又不是管理员，搁这装啥犊子呢？");
            }
        }
        return serverResponse;
    }
}
